<?php
require_once("$CFG->dirroot/config.php");
require_once ("$CFG->dirroot/lib/moodlelib.php");
require_once ("$CFG->dirroot/lib/dmllib.php");

global $CFG;

function block_matriculation_read_file($filename, &$user) {
	

	$newfilename=str_replace('.csv','('.time().')',$filename);	
	$newfilename.='.csv';
						
	if (file_exists($filename)) {
		 if ($dh = fopen ($filename, 'r')) {
			$i=0;
			while ($data_file=fgets($dh)) {
				if ($i!=0) {	
					$user[$i-1] = explode(';',$data_file);
					if (empty($user[$i-1][0]) || empty($user[$i-1][1]) || empty($user[$i-1][2]) || empty($user[$i-1][4]) || empty($user[$i-1][5]) || empty($user[$i-1][6])){
						return 3;
					}
				}
				$i++;
			}
			fclose($dh);
			if (file_exists($filename)) {
				rename($filename,$newfilename);
			}
			if ($i<=1)
				return 4; // el archivo esta vacio
			else	
				return 0; // se ha cargado completo
				
		 } else return 2; // no se ha podido abrir el archivo
		 
	} else return 1; // no existe el archivo
	
	return 0;
}

function block_matriculation_write_db($users) {
	
	$time=time();
	$i=0;
	$error=0;

	foreach ($users as $user) {
		
		$data = new object;
		
		$data->dni = $user[0];
		$data->firstname = $user[1];
		$data->lastname1 = $user[2];
		
		if (!empty($user[3]))
			$data->lastname2 = $user[3];
		else
			$data->lastname2 = '';
			
		if (!empty($user[4]))
			$data->email = $user[4];
		else
			$data->email = '';
			
		$data->cycle_code = $user[5];
		$data->module_code = $user[6];
		$data->center_code = $user[7];
		$data->time = $time;
		
		if (!insert_record('block_matriculation_temp', $data))
			$error=1;	
		
	}
	
	return $error;
}

function block_matriculation_compare_db_count($action) {
	
	
	if ($action==1) {	 // nº de altas nuevas
		$sql="SELECT count(*) as num FROM mdl_block_matriculation_temp WHERE (dni, cycle_code, module_code, center_code) not In ( SELECT dni, cycle_code, module_code, center_code FROM mdl_block_matriculation )";
	}
	
	if ($action==2) { // nº de bajas nuevas
		$sql="SELECT count(*) as num FROM mdl_block_matriculation WHERE (dni, cycle_code, module_code, center_code) not In ( SELECT dni, cycle_code, module_code, center_code FROM mdl_block_matriculation_temp )";
	}
	
	$results=get_record_sql($sql);
	
	return $results->num;
}

function block_matriculation_compare_db($action) {
	
	
	if ($action==1) {	
		$sql="SELECT * FROM mdl_block_matriculation_temp WHERE (dni, cycle_code, module_code, center_code) not In ( SELECT dni, cycle_code, module_code, center_code FROM mdl_block_matriculation ) ORDER BY cycle_code,module_code";
	}
	
	if ($action==2) {
		$sql="SELECT * FROM mdl_block_matriculation WHERE (dni, cycle_code, module_code, center_code) not In ( SELECT dni, cycle_code, module_code, center_code FROM mdl_block_matriculation_temp) ORDER BY cycle_code,module_code";
	}
	
	$results=get_records_sql($sql);
	
	return $results;
}

function block_matriculation_user_exist($dni) {
	
	if (get_record('block_matriculation_user','dni', $dni))
		return 1;
	else
		return 0;
	
}

function block_matriculation_write_user_db($user) {
	
	$time=time();
	$error=0;

	if (!block_matriculation_user_exist($user->dni)) { //existe el usuario
		//no existe el usuario
		$data_user = new object;
		$data_user->dni = $user->dni;
		$data_user->firstname = $user->firstname;
		$data_user->lastname1 = $user->lastname1;
		$data_user->lastname2 = $user->lastname2;
		$data_user->email = $user->email;			
		
		if (!insert_record('block_matriculation_user', $data_user));
			$error=1;						
	}
	
	$data_matriculation = new object;	
	$data_matriculation->dni = $user->dni;
	$data_matriculation->cycle_code = $user->cycle_code;
	$data_matriculation->module_code = $user->module_code;
	$data_matriculation->center_code = $user->center_code;
	
	
	if (!insert_record('block_matriculation', $data_matriculation));
		$error=1;				
		
	//se borra al usuario de la tabla temporal
	delete_records('block_matriculation_temp', 'dni', $user->dni, 'cycle_code', $user->cycle_code,'module_code',$user->module_code,'center_code',$user->center_code);
	
	
	return $error;
}


?>